<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>结算明细</span>
          </div>
        </div>
        <div class="mainor_form marginTop15">
          <div class="mainor_te">
            <el-button
              @click="
                () => {
                  this.exportbuVisible = true;
                }
              "
              class="mainor_tea"
              >导出</el-button
            >
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="单据号" prop="order_sn">
              <el-input
                v-model.trim="form.order_sn"
                placeholder="单据号"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="采购方式" prop="mode">
              <el-select
                v-model.trim="form.mode"
                placeholder="采购方式"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(modeList, index) in list.modeList"
                  :key="index"
                  :label="modeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="结算日期" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom'].resetFields()"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_num boxdq marginTop10">
          <div class="main_num_l box">
            <p>
              付款金额：
              <em>{{ pay_money_all }}</em>
            </p>
            <p>
              抹零金额：
              <em>{{ zero_amount_all }}</em>
            </p>
          </div>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection"></el-table-column>
            <el-table-column
              label="单据号"
              width="200"
              prop="related_purchase_order_sn"
            />
            <el-table-column label="单据类型" width="120" prop="status">
              <template slot-scope="scope">{{
                list.typeList[scope.row.type]
              }}</template>
            </el-table-column>
            <el-table-column label="付款金额" width="120" prop="pay_money" />
            <el-table-column label="抹零金额" width="120" prop="zero_amount" />
            <el-table-column label="付款方式" width="120" prop="status">
              <template slot-scope="scope">{{
                list.pay_type_list[scope.row.advice_settlement.pay_type]
              }}</template>
            </el-table-column>
            <el-table-column
              label="结算单号"
              prop="advice_settlement.order_sn"
            />
            <el-table-column label="结算日期" prop="">
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at, "{y}-{m}-{d}")
              }}</template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 导出 -->
    <el-dialog
      title="导出"
      :visible.sync="exportbuVisible"
      class="main_dialog dialog500"
    >
      <div v-if="selectArr.length == 0 ? true : false">
        <p class="exptitle">创建日期</p>
        <el-date-picker
          v-model="exportTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          @change="datepicker"
        >
        </el-date-picker>
        <!-- <el-tooltip
          class="item"
          effect="dark"
          content="时间范围只能选择一个月之内
          placement="top"
        >
          <i class="el-icon-question"></i>
        </el-tooltip> -->
      </div>
      <div v-if="selectArr.length == 0 ? true : false">
        <p class="exptitle company_name">采购方式</p>
        <el-select v-model.trim="mode" placeholder="采购方式" class="wid220">
          <el-option label="所有" value=""></el-option>
          <el-option
            v-for="(modeList, index) in list.modeList"
            :key="index"
            :label="modeList"
            :value="parseInt(index)"
          ></el-option>
        </el-select>
      </div>
      <div v-if="selectArr.length == 0 ? false : true" class="maintip">
        你已经选择了<span>{{ selectArr.length }}</span
        >行数据
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="exportbuVisible = false">取 消</el-button>
        <el-button type="primary" @click="exportConfirm" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  advice_getList,
  export_advice_detail,
} from "@/api/financial/financialRed";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { parseTime } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 时间
      parseTime,
      // 列表数据
      list: {
        list: [],
      },
      zero_amount_all: 0,
      pay_money_all: 0,
      listLoading: false,
      // 搜索
      form: {
        order_sn: "",
        start_time: "",
        end_time: "",
        mode: null,
      },
      createTime: [],
      pickerOptions: dateOption,

      // 导出
      selectArr: [],
      exportbuVisible: false,
      exportTime: "",
      mode: "",
    };
  },
  created() {
    // 列表
    this.advice_getList();
  },
  methods: {
    // 导出明细

    exportConfirm() {
      let ids = [];
      if (this.selectArr.length > 0) {
        this.selectArr.forEach((item) => {
          ids.push(item.id);
        });
      }
      ids = ids.join("-");
      let form = {
        ids: ids,
        start_time: "",
        end_time: "",
        mode: this.mode,
        order_sn: "",
      };
      if (this.exportTime) {
        form.start_time = this.exportTime[0];
        form.end_time = this.exportTime[1];
        delete form.ids;
      }
      if (!form.ids && !form.start_time) {
        this.$message.error("请选择导出的订单或者导出的时间");
        return;
      }
      export_advice_detail(form)
        .then((res) => {
          const link = document.createElement("a");
          // let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          let blob = new Blob([res], {
            type:
              "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "采购结算列表明细表";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
      this.exportbuVisible = false;
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.advice_getList();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.advice_getList();
    },
    changePage(page) {
      this.current.page = page;
      this.advice_getList();
    },
    // 列表
    advice_getList() {
      this.listLoading = true;
      let data = Object.assign(
        apiUrl().name,
        this.list_form(),
        this.current
      );
      advice_getList(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.pay_money_all = res.data.pay_money_all;
          this.zero_amount_all = res.data.zero_amount_all;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";

      return form;
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
      console.log(this.selectArr);
    },
    // 导出时间范围
    datepicker(val) {
      let start = "";
      let end = "";
      if (val) {
        start = new Date(val[0]).getTime();
        end = new Date(val[1]).getTime();
        if (end - start > 30 * 24 * 60 * 60 * 1000) {
          this.$message.error("选择范围不能超过一个月，请重新选择");
          this.exportTime = "";
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
}

// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
}
.company_name {
  padding-top: 15px;
}
.maintip {
  text-align: center;
  padding-top: 15px;
  font-size: 18px;
  span {
    display: inline;
    padding: 18px 0;
    color: red;
  }
}
.isexportCheckbox {
  margin-top: 15px;
}
.company_name_box {
  .company_name {
    padding-top: 0;
  }
}
</style>
